package com.voice.server.publish.device;

import com.alibaba.fastjson.JSONObject;
import com.voice.core.connection.entity.Stream;
import com.voice.core.connection.entity.StreamData;
import com.voice.server.command.impl.AudioTtlCommand;
import com.voice.server.connection.DeviceControllerConnection;
import com.voice.server.constant.RoleConstant;
import com.voice.server.publish.PublishHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Component("/device/pub/tts")
public class DeviceTtsHandler implements PublishHandler<DeviceControllerConnection> {

    @Autowired
    private AudioTtlCommand audioTtlCommand;

    @Override
    public void handle(Stream stream,StreamData streamData, DeviceControllerConnection connection){
        JSONObject jsonObject=JSONObject.parseObject(new String(streamData.readAllData()));
        if(jsonObject==null) return;
        String text=jsonObject.getString("text");
        if(StringUtils.isEmpty(text)) return;
        audioTtlCommand.execute(RoleConstant.rootUserId,connection.getDeviceKey(),text);
    }
}
